import {call, put, take} from "redux-saga/effects";
import {FETCH_DEVICES, REFRESH_DEVICES, SAVE_DEVICES} from "./const";
import {fetchDeviceList} from "../../services/device";

function* fetchDevices() {
    try {
        const rsp = yield call(fetchDeviceList);
        const deviceList = rsp.data;//rsp 调用接口拿到的数据
        yield put({
            type: SAVE_DEVICES,//保存的地方：姓名
            payload: {
                deviceList,
            },
        })
    } catch (e) {
        console.log(e);
    }
}


export default function* saga() {
    const {payload} = yield take(FETCH_DEVICES);
    yield call(fetchDevices);
    while (true) {
        yield take(REFRESH_DEVICES);
        yield call(fetchDevices);
    }
}
